package com.raising.modules.sys.controller;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.Dict;
import com.raising.modules.sys.entity.DictItem;
import com.raising.modules.sys.service.DictItemService;
import com.raising.modules.sys.service.DictService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JCacheUtils;
import com.raising.utils.JStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.ws.rs.POST;
import java.util.List;

/**
* 字典表 控制器
* @author gaoy
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/sys/dict")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;
    @Autowired
    private DictItemService dictItemService;

    @ModelAttribute
    public Dict get(@RequestParam(required = false) String id) {
        Dict entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = dictService.get(id);
        }
        if (entity == null) {
            entity = new Dict();
        }
        return entity;
    }
    /**
     * 跳转字典表列表
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/list")
    public String list(Dict dict,Model model, Page<Dict> page) {
        page.setEntity(dict);
        model.addAttribute("page", dictService.getPage(page));
        return "sys/dict/list";
    }

    /**
     * 查询字典表列表数据【json】
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/list.json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo listJson(Dict dict, Model model, Page<Dict> page,Integer pageNo) {
        page.setEntity(dict);
        return new ResultVo(ResultVo.SUCCESS, "成功",dictService.getPage(page));
    }
    /**
     * 查询字典表明细数据
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/listItems.json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo listItemsJson(DictItem dictItem, Model model, Page<Dict> page,Integer pageNo) {
        return new ResultVo(ResultVo.SUCCESS, "成功",dictItemService.getList(dictItem));
    }
    /**
     * 跳转字典表详情页面
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/view")
    public String view(Model model, String id) {
        model.addAttribute("dict", dictService.get(id));
        DictItem item = new DictItem();
        item.setDictId(id);
        List<DictItem> dictItemList = dictItemService.getList(item);
        model.addAttribute("dictItemList", dictItemList);
        return "sys/dict/view";
    }
    /**
     * 跳转创建页面
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Dict dict,Model model) {
        model.addAttribute("dict", dict);
        return "sys/dict/form";
    }
    /**
     * 新增数据
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Dict dict, RedirectAttributes redirectAttributes) {
        dictService.saveDictAndItems(dict);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/sys/dict/update?id="+dict.getId();
    }
    /**
     * 删除字典表明细数据项
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "/deleteItem.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo deleteItem(String id) {
        dictItemService.delete(id);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return new ResultVo(ResultVo.SUCCESS,"ok","ok");
    }
    /**
     * 跳转到更新页面
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Dict dict, Model model) {
        model.addAttribute("dict", dict);
        DictItem item = new DictItem();
        item.setDictId(dict.getId());
        List<DictItem> dictItemList = dictItemService.getList(item);
        model.addAttribute("dictItemList", dictItemList);
        return "sys/dict/form";
    }
    /**
     * 执行更新
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
	@RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Dict dict, RedirectAttributes redirectAttributes) {
        dictService.saveDictAndItems(dict);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/sys/dict/update?id="+dict.getId();
    }
    /**
     * 执行删除字典数据
     * @author GaoYuan
     * @param id 删除的对象标识
     * @datetime 2017/12/6 上午9:41
     */
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
        dictService.delete(id);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/sys/dict/list";
    }
}
